<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/13
 * Time: 14:02
 */
namespace  work {

    use app\ctr;
    use C\lib\Db;
    use C\lib\Zfbpay;
    use C\mod\worker;
    use C\mod\proute;
    use C\lib\verification;
    use C\mod\wprice;
    use C\mod\feedback;

    class apiPay extends ctr
    {
        /*
         * 支付宝支付接口
         */
        public function pay(){
            if (($post = $this->getAesPost()) && ($worker = $this->getLogWorker())) {
                $proute = new proute();
                $wprice=new wprice();
                $order_no = $this->build_order_no();
                if(empty($post['money'])){
                    $this->writeAesJson(0,'缺少金额参数！',1);
                }
                $res = Zfbpay::alpay($order_no, $post['money']);
                if ($res) {

                    try {
                        Db::beginTransaction();
                        $proute->set([
                            'worker_id' => $worker->id,
                            'time_add' => time(),
                            'price' => $post['money'],
                            'order_id' => $order_no,
                            'pay_type' => 'zfb',
                            'pay_state' => 0,
                            'remark' => 5//押金充值

                        ]);
                        $wprice->set([
                            'worker_id'=>$worker->id,
                            'price_mortgage'=>$post['money'],
                            'pay_state'=>0
                        ]);
                        $dbp=$proute->create();
                        $dbw=$wprice->create();


                        if ($dbw && $dbp) {
                            Db::commit();
                            $this->writeAesJson(1,['orderstring'=>$res],1);exit;
                        } else {
                            Db::rollBack();
                            $this->writeAesJson(0,'请求支付失败',1);exit;
                        }
                    } catch (\Exception $e) {
                        Db::rollBack(); // 执行失败，事务回滚
                        $this->writeAesJson(0,'请求支付失败',1);exit;
                    }
                }
            }
        }


        /*订单号*/
        private function build_order_no()
        {
            return 'DB' . date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
        }
        /*
         * 异步回调
         */
        public function notify(){
            Db::connect();
            $data = [];
            $GLOBALS['PAY_NOTIFY'] = $_POST ? $_POST : $_GET;
            $data = $GLOBALS['PAY_NOTIFY'];


//            $TxtFileName = "paytest.txt";
//            $TxtRes=fopen ($TxtFileName,"w+");
//            fwrite ($TxtRes,$data);

//            $fee=new feedback();
//            $fee->set([
//                'time_add'=>time(),
//                'context'=>$data
//            ]);
//            $fee->create();


            $arr=Zfbpay::zfbNotify($data);
            if(!empty($arr)){
                $this->updata($arr['order_no'],$arr['total_amount']);
            }
        }

        private function updata($order_no, $total_amount)
        {
            Db::connect();
            $wmoney = new wprice();
            $data=proute::get(['order_id'=>$order_no]);
            $wprice=wprice::get(['worker_id'=>$data->worker_id]);
            try {
                Db::beginTransaction();
                if($wprice){
                    $wprice->set([
                        'price_mortgage' => $total_amount,
                        'pay_state'=>1
                    ]);
                    $dbw = $wprice->update();
                }else{
                    $wprice->set([
                        'worker_id' => $data->worker_id,
                        'price_mortgage' => $total_amount,
                        'pay_state'=>1
                    ]);
                    $dbw = $wmoney->create();
                }
                $data->set([
                    'price' => $total_amount,
                    'pay_state' => 1
                ]);

                $dbp = $data->update();
                if ($dbw && $dbp) {
                    Db::commit();
                } else {
                    Db::rollBack();
                }
            } catch (\Exception $e) {
                Db::rollBack(); // 执行失败，事务回滚
            }
        }



    }
}
